#!/usr/bin/env python
# -- encoding utf-8 --
#
#  Created by Skiloop
#  Email: skiloop@126.com
#
from functools import partial

from PIL import Image

from captcha import resolve_captcha, resolve_binary


def unlean_image(image, lean_fun):
    height, width = image.shape
    for i in range(0, height):
        lean = lean_fun(i, height, width)
        image[i, :width - lean] = image[i, lean:]
        if lean != 0:
            image[i, width - lean:] = [255] * lean


def lean_fun(i, h, w):
    return (h - i) / 3


if __name__ == '__main__':
    options = ['--tessdata-dir', '/usr/share/', '-psm', '8']
    config = './config.txt'
    print resolve_captcha(Image.open('8-8.jpeg'), lang='chi_sim', options=options, config=config)

    config2 = './config2.txt'
    print resolve_binary('2x4.jpg', 120, lang='chi_sim', options=options, config=config2,
                         func=partial(unlean_image, lean_fun=lean_fun))
    print resolve_binary('0-3.jpg', 120, lang='chi_sim', options=options, config=config2,
                         func=partial(unlean_image, lean_fun=lean_fun))
    config3 = './config3.txt'
    print resolve_binary('2-0.jpg', 223, lang='chi_sim', options=options, config=config3)
